package xiaodou.core.WeiboData
{
	import com.sina.microblog.data.MicroBlogComment;
	
	import flash.utils.Dictionary;
	
	import spark.formatters.DateTimeFormatter;
	
	public class WeiboCommentsData
	{
		/**
		 *保存评论内容的数组 
		 */		
		protected var _AryComment:Array = new Array();
		
		protected var _TimeCursorFront:Object;
		
		protected var _TimeCursorBack:Object;
		
		protected var _OneDayCommentCounts:Dictionary = new Dictionary(); 
		
		protected var DateFormat:DateTimeFormatter = new DateTimeFormatter();
		
		
		public function WeiboCommentsData()
		{
			_TimeCursorFront = { date: new Date(2000,1,1), cursorCommentID:-1};
			_TimeCursorBack = { date: new Date(), cursorCommentID:-1};
			DateFormat.dateTimePattern = "yyyy-MM-dd";
		}
		
		public function RemoveAllData():void
		{
			_AryComment.splice(0);
			
			_TimeCursorFront = { date: new Date(2000,1,1), cursorCommentID:-1};
			_TimeCursorBack = { date: new Date(), cursorCommentID:-1};
		}
		
		
		/**
		 * 记录所存评论中时间游标，发布最早的评论的时间
		 */
		public function get TimeCursorBack():Date
		{
			return _TimeCursorBack.date;
		}
		
		/**
		 *记录所存评论中时间游标，发布最晚的评论的时间 
		 */
		public function get TimeCursorFront():Date
		{
			return _TimeCursorFront.date;
		}
		
		/**
		 * 记录所存评论中时间游标，发布最早的评论的ID
		 */
		public function get TimeCursorBackID():String
		{
			return _TimeCursorBack.cursorCommentID;
		}
		
		/**
		 *记录所存评论中时间游标，发布最晚的评论的ID 
		 */
		public function get TimeCursorFrontID():String
		{
			return _TimeCursorFront.cursorCommentID;
		}
		
		/**
		 *评论的总数
		 */
		public function get TotalNum():int
		{
			return _AryComment.length;
		}
		
		/**
		 *	根据时间段来获取评论信息 
		 * @param from 开始时间
		 * @param to   结束时间
		 * @return 
		 * 
		 */		
		public function GetCommentsByDate(from:Date, to:Date):Array
		{
			if(from < _TimeCursorBack.date)
				from = _TimeCursorBack.date;
			
			if(to > _TimeCursorFront.Date)
				to = _TimeCursorFront.Date;
			
			
			var ary:Array = new Array();
			for(var i:int = 0; i < _AryComment.length; ++ i)
			{
				var commentCreatedAt:Date = new Date(Date.parse(_AryComment[i].createdAt));
				if(commentCreatedAt >= from  && commentCreatedAt <= to)
				{
					ary.push(_AryComment[i]);
				}
			}
			
			return ary;
		}
		
		/**
		 *添加一条信息到记录中 
		 * @param status
		 * 
		 */		
		public function PushNewComment(comment:Object):void
		{
			var commentCreatedAt:Date = new Date(Date.parse(comment["created_at"]));
			var Length:int = _AryComment.push( new MicroBlogComment(comment));
			var Index:int = Length - 1;
			if(commentCreatedAt < TimeCursorBack)
			{
				_TimeCursorBack = {
					date		  :commentCreatedAt,
					cursorCommentID:comment["id"]
				}
			}
			
			if(commentCreatedAt > TimeCursorFront)
			{
				_TimeCursorFront = {
					date		  :commentCreatedAt,
					cursorCommentID:comment["id"]
				}
			}
			
			if(_OneDayCommentCounts[DateFormat.format(commentCreatedAt)] == null)
			{
				var obj:Array = new Array();
				obj.push(Index);
			}
			else
			{
				_OneDayCommentCounts[DateFormat.format(commentCreatedAt)].push(Index);
			}
		}
		
		/**
		 *获得某一天微博的评论量
		 * @param d 日期
		 * @return count
		 * 
		 */		
		public function GetOneDayCommentsCounts(d:Date):int
		{
			d = new Date( DateFormat.format(d).toString());
			return _OneDayCommentCounts[d.toString()].length;
		}

		
	}
}